package com.sky.financelake.ui.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.ViewModelProvider;

import com.bumptech.glide.Glide;
import com.sky.financelake.base.BaseToolA;
import com.sky.financelake.bean.entity.CommunityEntity;
import com.sky.financelake.databinding.ActivityCommunityDetailBinding;
import com.sky.financelake.viewmodels.remote.CommunityViewModel;

import java.io.IOException;
import java.util.List;

import cn.droidlover.xrichtext.ImageLoader;
import cn.droidlover.xrichtext.XRichText;
import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class CommunityDetailA extends BaseToolA {


    private ActivityCommunityDetailBinding binding;
    private CommunityViewModel communityViewModel;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityCommunityDetailBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        Integer id = getIntent().getIntExtra("id", 0);
        String title = getIntent().getStringExtra("title");

        communityViewModel = new ViewModelProvider(this).get(CommunityViewModel.class);

        Toolbar toolbar = binding.toolbarContainer.toolbar;

        initToolbar(toolbar,title);

        XRichText richText = binding.richText;

        communityViewModel.getCommunityById(id).observe(this, response -> {
            CommunityEntity entity = (CommunityEntity) response.getResult();
            richText.callback(new XRichText.Callback() {
                @Override
                public void onImageClick(List<String> urlList, int position) {
                    Log.i(TAG, "onImageClick: ");
                }

                @Override
                public boolean onLinkClick(String url) {
                    Log.i(TAG, "onLinkClick: ");
                    return false;
                }

                @Override
                public void onFix(XRichText.ImageHolder holder) {
                    Log.i(TAG, "onFix: ");
                }
            }).imageDownloader(new ImageLoader() {
                @Override
                public Bitmap getBitmap(String url) throws IOException {
                    Bitmap bmp = null;
                    try {
                        bmp = Glide.with(CommunityDetailA.this).asBitmap().load(url).submit().get();
                    } catch (Exception e) {
                        Log.i(TAG, "getBitmap: " + e.getLocalizedMessage());
                    }
                    return bmp;
                }
            }).text(entity.getContent());

        });

    }


    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
}
